import pygame
from pygame.locals import *
import socket
import time

#command window
black = (0,0,0)
white = (255,255,255)

pygame.init()
size=width,height=640,391;
screen=pygame.display.set_mode(size);
pygame.display.set_caption("plpbot control window")
pygame.mouse.set_visible(1)
pygame.key.set_repeat(60,0)
not_exit = True

TCP_IP = "139.78.91.160"
TCP_PORT = 1337
BUFFER_SIZE = 1024

s = socket.create_connection((TCP_IP, TCP_PORT))

plp = pygame.image.load("plpbot.png").convert()
up = pygame.image.load("up.png").convert()
down = pygame.image.load("down.png").convert()
left = pygame.image.load("left.png").convert()
right = pygame.image.load("right.png").convert()
first_gear = pygame.image.load("firstgear.png").convert()
second_gear = pygame.image.load("secondgear.png").convert()
third_gear = pygame.image.load("thirdgear.png").convert()
reverse_gear = pygame.image.load("reversegear.png").convert()

gear = 1
left_motor = chr(64)
right_motor = chr(192)
header = "#"
clock=pygame.time.Clock()

while not_exit:
       
       
        #for event in pygame.event.get():
       
        screen.blit(plp,[0,0])
        clock.tick(20)
        pygame.event.pump()
	key = pygame.key.get_pressed() 		      

        if key[pygame.K_ESCAPE]:
            not_exit = False
                       
        elif key[pygame.K_1]:
            gear = 1
            print "1"
            screen.blit(first_gear,[541,193])              
       
        elif key[pygame.K_2]:
            gear = 2
            print "2"
            screen.blit(second_gear,[541,119])
       
        elif key[pygame.K_3]:
            gear = 3
            print "3"
            screen.blit(third_gear,[541,45])

               
       
        elif key[pygame.K_r]:
            gear = 5
            print "R"
            screen.blit(reverse_gear,[541, 267])
       

        elif gear == 1 and key[pygame.K_UP]:
            print "Forward in gear 1"
            left_motor = chr(85)
            right_motor = chr(213)
            screen.blit(up,[234,62])
                       
        elif gear == 2 and key[pygame.K_UP]:
            print "Forward in gear 2"
            left_motor = chr(105)
            right_motor = chr(233)
            screen.blit(up,[234,62])
                       
        elif gear == 3 and key[pygame.K_UP]:
            print "Forward in gear 3"
            left_motor = chr(126)
            right_motor = chr(254)
            screen.blit(up,[234,62])
                       
                
                
        elif gear == 5 and key[pygame.K_DOWN]:
            print "Go in reverse"
            left_motor = chr(32)
            right_motor = chr(159)
            screen.blit(down,[234,216])
                       
        elif gear == 5 and key[pygame.K_LEFT]:
            print "Go in left in reverse"
            left_motor = chr(32)
            right_motor = chr(224)
            screen.blit(left,[157,139])
            screen.blit(down,[234,216])

        elif gear == 5 and key[pygame.K_RIGHT]:
            print "Go in right in reverse"
            left_motor = chr(96)
            right_motor = chr(160)
            screen.blit(right,[311,139])
            screen.blit(down,[234,216])
       
        elif gear == 1 and key[pygame.K_LEFT]:
            print "Left in gear 1"
            left_motor = chr(43)
            right_motor = chr(213)
            screen.blit(left,[157,139])
                       
        elif gear == 2 and key[pygame.K_LEFT]:
            print "Left in gear 2"
            left_motor = chr(43)
            right_motor = chr(213)
            screen.blit(left,[157,139])
                       
        elif gear == 3 and key[pygame.K_LEFT]:
            print "Left in gear 3"
            left_motor = chr(43)
            right_motor = chr(213)
            screen.blit(left,[157,139])
                       
                
                       
        elif gear == 1 and key[pygame.K_RIGHT]:
            print "Right in gear 1"
            left_motor = chr(85)
            right_motor = chr(171)
            screen.blit(right,[311,139])
                       
        elif gear == 2 and key[pygame.K_RIGHT]:
            print "Right in gear 2"
            left_motor = chr(85)
            right_motor = chr(171)
            screen.blit(right,[311,139])
                       
        elif gear == 3 and key[pygame.K_RIGHT]:
            print "Right in gear 3"
            left_motor = chr(171)
            right_motor = chr(129)
            screen.blit(right,[311,139])
                       
                

        else:
            left_motor = chr(64)
            right_motor = chr(192)

        if gear == 1:
            screen.blit(first_gear,[541,193])
        elif gear == 2:
            screen.blit(second_gear,[541,119])
        elif gear == 3:
            screen.blit(third_gear,[541,45])
        elif gear == 5:
            screen.blit(reverse_gear,[541,267])
               
	s.send(header)
	s.send(left_motor)
	s.send( right_motor)
	time.sleep(.01)
	font = pygame.font.Font(None, 25)
	pygame.display.flip()
                                       
pygame.quit()
exit(0)

